<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_upprice/model')->方法名();
*/
namespace jy_upprice;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function daoru($post=array(),$is2=false){
		if(!$post['excel']){
			exi('请先上传excel文件！','error');
		}
		$localexcel=cfc('files')->local($post['excel']);
		$excel=cfc('tools')->importExcel2($localexcel,'');
		//print_r($excel);exit;
		$excel[1]=$excel[2];
		unset($excel[2]);
		$res=$this->get_csvitems($excel);
		//print_r($res);exit;
		if(!$res[0]['系列'] && !$res[0]['型号']){
			$excel=cfc('tools')->importExcel2($localexcel,true);
			$excel[1]=$excel[2];
			unset($excel[2]);
			$res=$this->get_csvitems($excel);
			if(!$res[0]['系列'] && !$res[0]['型号']){
				cfc('files')->source_del($post['excel']);
				exi('导入文件异常，内容为空！','error');
			}
		}
		cfc('files')->source_del($post['excel']);
		$cdata=$this->csv_add($res,$post,0,$is2);
		if($is2){
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'pcate'=>$post['pcate'],
				'type'=>2,
				'uptype'=>$post['up2type'],
				'fee'=>$post['up2type']==1?$post['up2jianfee']:$post['up2jianrate'],
				'createtime'=>TIMESTAMP
			);
		}else{
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'pcate'=>$post['pcate'],
				'type'=>1,
				'uptype'=>$post['up1type'],
				'fee'=>$post['up1type']==1?$post['up1jianfee']:$post['up1jianrate'],
				'createtime'=>TIMESTAMP
			);
		}
		pdo_insert('jy_upprice_log',$insert);
		if($cdata){
			cache_write('kcdata',$cdata,'jy_upprice');
			exi('导入成功（查看本次处理的商品）！','','/cdata/');
		}else{
			exi('导入成功！','','/upprice1/');
		}
	}
	public function csv_add($rows,$post,$xlsx=0,$is2=0){
		$cdata=array();
		$update=array();
		$jianfee=$is2?$post['up2jianfee']:$post['up1jianfee'];
		$jianrate=$is2?$post['up2jianrate']:$post['up1jianrate'];
		foreach($rows as $item){
			$pipeis=$this->pipeigoodsid($item,$post);
			if(!$pipeis['goodsids']){
				continue;
			}
			if(!$item['报价']){
				continue;
			}
			if(!$post['up1type']){
				$update['price']=$item['报价'];
				$update['costprice']=$item['报价'];
			}elseif($post['up1type']==1){
				$update['price']=$item['报价']+$jianfee;
				$update['costprice']=$item['报价'];
			}elseif($post['up1type']==2){
				$addfee=$jianrate*$item['报价'];
				$update['price']=$item['报价']+$addfee;
				$update['costprice']=$item['报价'];
			}
			if($is2){
				unset($update['costprice']);
			}
			if($pipeis['optionids']){
				pdo_update('jy_weishop_goods_option',$update,array('id'=>$pipeis['optionids'],'uniacid'=>$_SESSION['uniacid']));
			}elseif($pipeis['goodsids']){
				pdo_update('jy_weishop_goods',$update,array('id'=>$pipeis['goodsids'],'uniacid'=>$_SESSION['uniacid']));
			}
			$cdata[]=array('pipeis'=>$pipeis,'update'=>$update);
		}
		return $cdata;
	}
	public function pipeigoodsid($item=array(),$postdata=array()){
		$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
		if($postdata['pcate']){
			$params['pcate']=$postdata['pcate'];
		}
		if($postdata['ccate']){
			$params['ccate']=$postdata['ccate'];
		}
		if($postdata['scate']){
			$params['scate']=$postdata['scate'];
		}
		if($item['系列']){
			$params['title like']='%'.trim($item['系列']).'%';
			$goodsids=pdo_getall('jy_weishop_goods',$params,'id','id');
		}
		//print_r(array('goodsids'=>$params,'item'=>$item));exit;
		if($item['型号'] && $goodsids){
			$goodsids=array_keys($goodsids);
			$params['title like']='%'.trim($item['型号']).'%';
			$params['id']=$goodsids;
			$goodsids=pdo_getall('jy_weishop_goods',$params,'id','id');
		}
		if($item['标题关键词'] && $goodsids){
			$goodsids=array_keys($goodsids);
			$params['title like']='%'.trim($item['标题关键词']).'%';
			$params['id']=$goodsids;
			$goodsids=pdo_getall('jy_weishop_goods',$params,'id','id');
		}
		
		$specs=array();
		if(!$goodsids){
			return array('goodsids'=>0,'optionids'=>0);
		}
		$goodsids=array_keys($goodsids);
		$params=array('uniacid'=>$_SESSION['uniacid'],'goodsid'=>$goodsids);
		if($item['规格1']){
			$params['title']=trim($item['规格1']);
			$specid=pdo_getcolumn('jy_weishop_goods_spec_item',$params,'id');
			if($specid){
				$specs[]=array('id'=>$specid);
			}
		}
		if($item['规格2']){
			$params['title']=trim($item['规格2']);
			$specid=pdo_getcolumn('jy_weishop_goods_spec_item',$params,'id');
			if($specid){
				$specs[]=array('id'=>$specid);
			}
		}
		if($item['规格3']){
			$params['title']=trim($item['规格3']);
			$specid=pdo_getcolumn('jy_weishop_goods_spec_item',$params,'id');
			if($specid){
				$specs[]=array('id'=>$specid);
			}
		}
		if($specs){
			paixu($specs,'id');
			$specsids=array();
			foreach($specs as $sp){
				$specsids[]=$sp['id'];
			}
			$specsids=implode('_',$specsids);
			unset($params['title']);
			$params['specs']=$specsids;
			$optionids=pdo_getall('jy_weishop_goods_option',$params,'id','id');
			$optionids=$optionids?array_keys($optionids):-1;
		}
		//print_r($specs);exit;
		return array('goodsids'=>$goodsids,'optionids'=>$optionids);
	}
	public function get_csvitems($rows){
			$num = count($rows);
			$i = 0;
			$colsIndex = $rows[1];
			$rows = array_slice($rows, 1, count($rows) - 1);
			$items = array();
			$num = 0;
			$lastkey=array();
			foreach ($rows as $rownu => $rv) {
				$item = array();
				foreach ($colsIndex as $cols => $cv){
					if($rv[$cols]){
						$lastkey[$cols]=$rv[$cols];
						$item[$cv]=$rv[$cols];
					}else{
						$item[$cv]=$lastkey[$cols];
					}
				}
				$items[] = $item;
				++$num;
			}
			return $items;
	}
}
?>